#include("/common/_layout.html")
#@layout()
#define layoutHead()
	<title>菜单编辑</title>
#end
#define layoutBody()
	<body>
		<div class="wrap-container">
			<form class="layui-form" style="width: 90%;padding-top: 20px;">
				<input type="hidden" name="menu_id">
					<div class="layui-form-item">
						<label class="layui-form-label">所属应用：</label>
						<div class="layui-input-block">
							<select id="app_id" name="app_id" lay-verify="required">
								<option value=""></option>
							</select>
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">上级：</label>
						<div class="layui-input-block">
							<select id="parent_id" name="parent_id" lay-verify="required">
								<option value="0">作为一级菜单</option>
							</select>
						</div>

					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">菜单名称：</label>
						<div class="layui-input-block">
							<input type="text" name="menu_name" required lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">
						</div>

					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">绝对URL：</label>
						<div class="layui-input-block">
							<input type="text" name="menu_url"  placeholder="请输入URL" autocomplete="off" class="layui-input">
						</div>

					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">排序：</label>
						<div class="layui-input-block">
							<input type="text" name="menu_sort" placeholder="请输入排序" autocomplete="off" class="layui-input">
						</div>

					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">图标：</label>
						<div class="layui-input-block">
							<input type="text" name="icon_code"  placeholder="请输入图标code" autocomplete="off" class="layui-input">
						</div>

					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">可见：</label>
						<div class="layui-input-block">
							<input type="radio" name="is_hide" value="0" title="显示" checked>
							<input type="radio" name="is_hide" value="1" title="隐藏">
						</div>

					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">状态：</label>
						<div class="layui-input-block">
							<input type="radio" name="menu_state" value="1" title="启用" checked>
							<input type="radio" name="menu_state" value="0" title="禁用">
						</div>

					</div>

					<div class="layui-form-item">
						<div class="layui-input-block">
							<button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo">立即提交</button>
							
						</div>
					</div>
				</form>
		</div>

		<script src="/static/js/jquery.formHelp.js" type="text/javascript" charset="utf-8"></script>
		<script>
			//Demo
			layui.use(['form', 'layer', 'laytpl'], function() {
				var form = layui.form();
				var layer = layui.layer;
				var laytpl = layui.laytpl;
				form.render();			
				
				//监听提交
				form.on('submit(formDemo)', function(formData) {					
					sendPost("/api/menu/save", formData.field, function(res) {
						console.log(res);
						layer.alert(res.msg, {
							title: '信息提示'
						}, function(idx){
							if (res.code==200){
								//parent.refresh();
								parent.reloadiFrame();
							}
							
						});
					});
					return false;
				});
				
				// 初始化上级菜单
				function filterListMenu(listmenu, pid, level){
					var menu_node1 = listmenu.filter(function (e) { return e.parent_id == pid; });
					for (var i = 0; i < menu_node1.length; i++) {
						var shtml = '<option value="'+menu_node1[i].menu_id+'">';
						for(j=0;j<level;j++){
							shtml += '&nbsp;&nbsp;&nbsp;&nbsp;';
						}
						if (level>0){
							shtml += '├─';
						}
						shtml += menu_node1[i].menu_name + '</option>';
						document.getElementById('parent_id').innerHTML += shtml;
						// 迭代
						filterListMenu(listmenu, menu_node1[i].menu_id, level+1);
					}
				}
				
				// 页面初始化
				function page_init(){
					var iid = getUrlParam('id');
					var so_app_id = getUrlParam('so_app_id');
					//console.log('iid:'+iid);
					if (iid===null || iid === 'undefined'){
						iid = 0;
					}
					// 加载初始化编辑数据
					sendGet("/api/menu/getMenu", {
						id : iid,
						so_app_id: so_app_id
					}, function(res) {
						//console.log(res);
						
						// 所属应用
						bindSelectJSONProps("app_id", res, "listapp", "app_name", "app_id", 1);
						
						
						if (so_app_id > 0){
							$('#app_id').val(so_app_id);
						}
						
						// 上级
						if (res.hasOwnProperty("listmenu")) {
							filterListMenu(res.listmenu, 0, 0);
							
							var pid = getUrlParam('pid');
							if (pid!=null && pid != 'undefined'){
								$('#parent_id').val(pid);
							}
						}
						form.render('select');
						
						if (res.hasOwnProperty("menudata")) {
							$("form.layui-form").setForm(res.menudata);
						}
						form.render();
					});
				}
				
				page_init();
			});
		</script>
	</body>

#end